let ycmjGame = require('YcmjGame');

class Game {
    
    constructor(opts){
        this.http = opts.http;
        this.user = opts.user;
        this.audioMgr = opts.audioMgr;
        this.isConnectServer = false;
        this.curGame = null;
        this.onGameEvnet();
    };
    onGameEvnet(){
        cc.game.on(cc.game.EVENT_HIDE, function(){
            console.log("cc.game.EVENT_HIDE==========>游戏进入后台");
        },this);
        cc.game.on(cc.game.EVENT_SHOW, function(){
            console.log("cc.game.EVENT_SHOW==========>重新返回游戏");
        },this);
        pomelo.on('close',(e)=>{
            cc.log('game=========close====>');
            this.isConnectServer = false;
            cc.log('触发重连,io-error');
            let data = {
                account: this.user.account,
                password: this.user.password
            };
            this.guest_login(data);
            //this.guest_login()
        });
        pomelo.on('io-error',(e)=>{
            cc.log('game=========io-error====>');
            this.isConnectServer = false;
            cc.log('触发重连,io-error');
        });
        pomelo.on('heartbeat timeout',(e)=>{
            cc.log('game ===== > heartbeat timeout====>');
            this.isConnectServer = false;
        });
    };
    openTip(tipEvent){
        cc.loader.loadRes('perfabs/tip',(err,res)=>{
            if(err){
                return;
            };
            let fab = cc.instantiate(res);
            let canvas = cc.find('Canvas');
            canvas.addChild(fab);
            let fabScript = fab.getComponent('Tip');
            fabScript.init(tipEvent);
        });
    };
    guest_register(accountInfo){
        this.http.request({
            //url:'http://106.54.77.145:3000/user/register',
            url:'http://106.54.77.145/user/register',
            //url:'http://127.0.0.1:3000/user/register',
            method: 'POST',
            data:accountInfo
        },(err,data)=>{
            if(err){
                this.openTip({
                    content:'网络连接失败',
                    ok_call:null,
                    cancel_call:null
                });
                return;
            }
            console.log(data);
            console.log('err=====>',err);
        });
    };
    guest_login(accountInfo){
        this.http.request({
            //url:'http://106.54.77.145:3000/user/login',
            url:'http://106.54.77.145/user/login',
            //url:'http://127.0.0.1:3000/user/login',
            method: 'POST',
            data:accountInfo
        },(err,data)=>{
            if(err){
                this.openTip({
                    content:'网络连接失败',
                    ok_call:null,
                    cancel_call:null
                });
                return;
            };
            if(data){
                data = JSON.parse(data);
                this.user.account = accountInfo.account;
                this.user.password = accountInfo.password;
                this.user.token = data.token;
                pomelo.init({
                    host:'127.0.0.1:3050' 
                  //host:'106.54.77.145:3050'
                },()=>{
                    pomelo.request('connector.entryHandler.login', {token: data.token}, (res)=>{
                        if(res.code != -5){
                            let msg =res.msg;
                            if(msg){
                                this.user.role.coin = msg.coin;
                                this.user.role.diamond = msg.diamond;
                                this.user.role.img = msg.img;
                                this.user.role.sex = msg.sex;
                                this.user.role.name = msg.name;
                                this.user.role.gems = msg.gems;
                                this.user.role.uid = msg.uid;
                                this.user.role.gameId = msg.gameId;
                                if(msg.serverType == 'ycmj'){
                                    let tid = msg.tid;
                                    pomelo.request('hall.hallHandler.joinRoom',{tid:tid},function(res){
                                        if(res.code != -5 && res.code != 500){
                                            console.log('res.msg----hall.hallHandler.joinRoom->',res.msg);
                                            let curgame = ycmjGame.getInstance(res.msg);
                                            if(curgame){
                                                this.curGame = curgame;
                                                cc.director.loadScene('Ycmj');
                                            };
                                        }else{
                        
                                        }
                                    });
                                }else{
                                    cc.director.loadScene('Hall');
                                }
                            }
                        }else{

                        }
                    });
                });
            }
        });
    };
    static getInstance(opts){
        if(!this.instance){
            this.instance = new Game(opts);
        }; 
        return this.instance;
    }
}
module.exports = Game;
